home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8246 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.mainelink.net!usenet
  2. From: dgarrets@mainelink.com (David Garretson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ for loop
  5. Date: Fri, 16 Feb 1996 03:43:11 GMT
  6. Organization: Internet Maine Inc.
  7. Message-ID: <4g0ue8$ct0@news.mainelink.net>
  8. References: <4flhm3$t3a@ulysses.midtown.net>
  9. NNTP-Posting-Host: bath-19.mainelink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. samwise@midtown.net (Rick) wrote:
  13.  
  14. >I'm in my second week in a C++ programming class.  I like it, its a
  15. >lot like Pascal.  I want to draw a box, so I converted an old Pascal
  16. >routine that I wrote.   I can't find a good explanation on the   for
  17. >command.  Wonder what I am doing wrong.  I hate to wait for Monday to
  18. >ask.  This is what I am trying:
  19.  
  20. >for(I=2;I >= 79;I++)
  21. >    {
  22. >         gotoxy(I,1);
  23. >         printf("-"); // #205            print #205
  24. >    };
  25. >I want to do like a FOR NEXT in BASIC or For Do in Pascal. I want to
  26. >count from 2 to 79 and store the incremented value each time in I,
  27. >using it for my gotoxy() screen position
  28. >Thanks
  29.  
  30.  
  31.  
  32. >...Give me back my data ya dirtbag machine!
  33.  
  34. The best way that I know to do this is with a nested for loop.  The
  35. outer for loop determines the height of the box and the inner for loop
  36. determines what gets displayed on the screen.  The following will
  37. display a box that is filled in to display a box that is not filled in
  38. you need to add some if, else if statements:
  39.  
  40. //box filled with X's
  41.  
  42. for( int i =0; i <= 10; i++) {
  43.     for( int a=0; a<=10; a++) {  //prints ten X's 
  44.         printf( "X");
  45.     }
  46.     printf("\n");  //starts a new line
  47. }
  48.  
  49. To print a 'empty' box you can try this:
  50.  
  51. for( int i=0; i<=10; i++) {
  52.     if( i==0 || i==10) {  //if first line or last
  53.         for( int a=0; a<=10; a++) {
  54.             printf("X");  // fill entire line with X's
  55.         }
  56.     }else {        //if any line other than first line or last    
  57.         for( int a=0; a<=10; a++) {
  58.             if( a == 0 || a==10) {  // and the first or last position in the 
  59.                 printf("X");  //line print an X
  60.             }
  61.         }
  62.     }
  63.     printf("\n"); //start a new line 
  64. }
  65.  
  66. The code you have written above would only print a single line of
  67. dashes, also the l >= 79 will cause the for loop to exit after one
  68. >for(I=2;I >= 79;I++)  
  69. iteration because it will evaluate as false as soon as it is
  70. evaluated.
  71.  
  72.